//
//  UIImage+Loader.m
//  ImageLoader
//
//  Created by Le Ngoc Thanh on 8/23/13.
//  Copyright (c) 2013 Le Ngoc Thanh. All rights reserved.
//

#import "UIImage+Loader.h"
#import "HMImageDownloadOperation.h"
#import "HMStoreImage.h"

@implementation UIImage (Loader)
+ (NSOperationQueue *) lowQueue{
    static NSOperationQueue *queue = nil;
    if (queue == nil) {
        queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:1];
    }
    return queue;
}
+ (NSOperationQueue *) hightQueue{
    static NSOperationQueue *queue = nil;
    if (queue == nil) {
        queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:4];
    }
    return queue;
}
+ (UIImage *)loadImageUrl:(NSString *)onlUrl typeLoader:(TYPELOAD)type isOverride:(BOOL)override block:(void (^)(void))block{
    return [UIImage loadImageUrl:onlUrl local:nil typeLoader:type isOverride:override block:block];
}
+ (UIImage *)loadImageUrl:(NSString *)onlUrl local:(NSString *)local typeLoader:(TYPELOAD)type isOverride:(BOOL)override block:(void (^)(void))block{
    UIImage *image = [[HMStoreImage share] imageForOnlineUrl:onlUrl];
    if (!image) {
        NSOperationQueue *queue = [UIImage hightQueue];
        NSOperationQueuePriority pri;
        switch (type) {
            case TYPELOAD_LOWPRIORITY:{
                queue = [UIImage lowQueue];
                pri = NSOperationQueuePriorityNormal;
                break;
            }
            case TYPELOAD_HIGHTPRIORITY:{
                queue = [UIImage hightQueue];
                pri = NSOperationQueuePriorityHigh;
                break;
            }
            default:
                break;
        }
        
        HMImageDownloadOperation *oper = [[HMImageDownloadOperation alloc] init];
        oper.imageUrl = onlUrl;
        oper.localUrl = local;
        [oper setQueuePriority:pri];
        [queue addOperation:oper];
    }
    return image;
}
#pragma mark -
+ (UIImage *)loadImageUrl:(NSString *)onlUrl local:(NSString *)local typeLoader:(TYPELOAD)type receiverObject:(NSObject *)object receiverNotiMethod:(SEL)method removeAllOldRegistNotification:(BOOL)remove isOverride:(BOOL)override{
    if (remove) {
        [[NSNotificationCenter defaultCenter] removeObserver:object];
    }
    UIImage *image = [UIImage loadImageUrl:onlUrl local:local typeLoader:type isOverride:override block:nil];
    if (!image) {
        [[NSNotificationCenter defaultCenter] addObserver:object selector:method name:onlUrl object:nil];
    }
    return image;
}
+ (UIImage *)loadImageUrl:(NSString *)onlUrl local:(NSString *)local typeLoader:(TYPELOAD)type receiverObject:(NSObject *)object receiverNotiMethod:(SEL)method removeOldRegistNotification:(NSString *)name isOverride:(BOOL)override{
    if ([name length]>0) {
        [[NSNotificationCenter defaultCenter] removeObserver:object name:name object:nil];
    }
    UIImage *image = [UIImage loadImageUrl:onlUrl local:local typeLoader:type isOverride:override block:nil];
    if (!image) {
        [[NSNotificationCenter defaultCenter] addObserver:object selector:method name:onlUrl object:nil];
    }
    return image;
}
+ (UIImage *)imageLocalFromOnlineUrl:(NSString *)url{
    return [[HMStoreImage share] imageForOnlineUrl:url];
}
@end
